package com.tyto.training;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("stockDao")
@Transactional
public class StockDaoImpl implements StockDao {
	@Autowired
	private SessionFactory sessionFactory;

	public Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public void save(Stock stock) {
		getCurrentSession().save(stock);
	}

	public void update(Stock stock) {
		getCurrentSession().update(stock);
	}

	public void delete(Stock stock) {
		getCurrentSession().delete(stock);
	}

	public Stock findByStockCode(String stockCode) {
		Query query = getCurrentSession().createQuery("from Stock where stockCode=:stockCode");
		query.setParameter("stockCode", stockCode);
		List<Stock> list = query.list();
		return list.get(0);
	}

}
